//
//  filter.m
//  storeapp
//
//  Created by huangyi on 13-5-20.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "UIPickerListView.h"
#import "toolbox.h"

@interface UIPickerListView ()

@property(nonatomic,strong)NSDictionary *activeRow;
@property(nonatomic,strong)NSMutableDictionary *query;
@property(nonatomic,strong)NSArray *list;

@end

@implementation UIPickerListView

-(id)initWithArray:(NSArray *)array{
    self=[self initWithStyle:UITableViewStyleGrouped];
    if (self) {
        self.list=array;
    }
    return self;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.query=[NSMutableDictionary dictionary];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onButtonClick:)];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onButtonClick:)];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)show{
    if (self.delegate!=nil) {
        UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:self];
        nav.modalPresentationStyle=UIModalPresentationFormSheet;
        [self.delegate presentViewController:self.navigationController animated:YES completion:nil];
    }
}
-(void)onButtonClick:(id)sender{
    if (sender==self.navigationItem.leftBarButtonItem) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
    if (sender==self.navigationItem.rightBarButtonItem) {
        if ([self.delegate respondsToSelector:@selector(pickerListView:didPickWithDictionary:)]) {
            if (self.query.count==0) {
                for (NSArray *section in self.list) {
                    for (NSDictionary *kv in section) {
                        NSString *key=kv.allKeys.firstObject;
                        NSArray *val=kv.allValues.firstObject;
                        self.query[key]=val.firstObject;
                    }
                }
            }
            [self.delegate pickerListView:self didPickWithDictionary:self.query];
        }
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView==self.tableView) {
        return self.list.count;
    }else{
        return 1;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.tableView) {
        NSArray *array=self.list[section];
        return array.count;
    }else{
        NSArray *val=self.activeRow.allValues.firstObject;
        return val.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==self.tableView) {
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"row"];
        if (cell==nil) {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"row"];
        }
        NSDictionary *item=self.list[indexPath.section][indexPath.row];
        NSString *key=item.allKeys.firstObject;
        NSArray *val=item.allValues.firstObject;
        cell.textLabel.text=key;
        if (self.query[key]) {
            cell.detailTextLabel.text=self.query[key];
        }else{
            cell.detailTextLabel.text=val.firstObject;
        }
        return cell;
    }else{
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"choice"];
        if (cell==nil) {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"choice"];
        }
        NSArray *val=self.activeRow.allValues.firstObject;
        cell.textLabel.text=val[indexPath.row];
        return cell;
    }
    return nil;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (tableView==self.tableView) {
        self.activeRow=self.list[indexPath.section][indexPath.row];
        NSString *title=self.activeRow.allKeys.firstObject;
        UITableViewController *picker=[[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
        picker.tableView.dataSource=self;
        picker.tableView.delegate=self;
        picker.title=title;
        [self.navigationController pushViewController:picker animated:YES];
    }else{
        NSString *key=self.activeRow.allKeys.firstObject;
        NSArray *array=self.activeRow.allValues.firstObject;
        NSString *val=array[indexPath.row];
        self.query[key]=val;
        self.activeRow=nil;
        [self.navigationController popViewControllerAnimated:YES];
        [self.tableView reloadData];
    }
}

@end
